from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=70)
    email = models.EmailField()

    def __unicode__(self):
        return self.name


class Publisher(models.Model):
    name = models.CharField(max_length=30, unique=True)
    website = models.URLField(blank=True)

    def __unicode__(self):
        return self.name


class Book(models.Model):
    title = models.CharField(max_length=100, unique=True)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()
    description = models.TextField()
    image_url = models.URLField()
    price = models.DecimalField(max_digits=8, decimal_places=2)
    ISBN = models.IntegerField(unique=True)

    def __unicode__(self):
        return self.title


class Order(models.Model):
    name = models.CharField(max_length=50)
    address = models.TextField()
    email = models.EmailField()


class LineItem(models.Model):
    book = models.ForeignKey(Book)
    order = models.ForeignKey(Order)
    unit_price = models.DecimalField(max_digits=8, decimal_places=2)
    quantity = models.IntegerField()

    def title(self):
        return self.book.title
